/*
 * This file is part of Grocy Android.
 *
 * Grocy Android is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Grocy Android is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Grocy Android. If not, see http://www.gnu.org/licenses/.
 *
 * Copyright (c) 2020-2023 by Patrick Zedler and Dominic Zedler
 */
package com.gitee.wsl.common.zxing

import android.app.Activity
import android.content.pm.ActivityInfo
import android.view.WindowManager
import androidx.lifecycle.LiveData

abstract class EmbeddedFragmentScanner(private val activity: Activity) {
    abstract fun setScannerVisibilityLive(scannerVisibilityLive: LiveData<Boolean>)
    abstract fun setScannerVisibilityLive(scannerVisibilityLive: LiveData<Boolean>, supressNextScanStart: Boolean)

    abstract fun onResume()
    abstract fun onPause()
    abstract fun onDestroy()
    abstract fun stopScanner()
    abstract fun startScannerIfVisible()
    abstract fun toggleTorch()
    fun lockOrUnlockRotation(scannerIsVisible: Boolean) {
        if (scannerIsVisible) {
            activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LOCKED
        } else {
            activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_FULL_USER
        }
    }

    // for MLKit scanner class
    fun keepScreenOn(keepOn: Boolean) {
        if (keepOn) {
            activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        } else {
            activity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        }
    }

    fun interface BarcodeListener {
        fun onBarcodeRecognized(rawValue: String)
    }
}